#!/bin/sh

#
# A set of useful functions to be sourced in each test
#

copy_configs()
{
    COMPONENTS="susan-dir susan-sd susan-fd bconsole tray-monitor"
    CONFIGDIRS="BASE ${TestName} $@"
    FOUND=""

    for component in $COMPONENTS; do
        # try to find a config file, store the most specific.
        FOUND=""
        for directory in $CONFIGDIRS; do
            if [ -f ${rconfigs}/${directory}/${component}.conf ]; then
                FOUND=${rconfigs}/${directory}/${component}.conf
            fi
        done
        if [ "$FOUND" ]; then
            /bin/cp ${FOUND} ${conf}
        else
            # copy all config files from subdirectories,
            # start with the most generic.
            for directory in $CONFIGDIRS; do
                if [ -d ${rconfigs}/${directory}/${component}.d ]; then
                    /bin/cp -r ${rconfigs}/${directory}/${component}.d ${conf}
                fi
            done
        fi
    done

    # copy certificates
    for directory in $CONFIGDIRS; do
        if [ -d ${rconfigs}/${directory}/tls/ ]; then
            /bin/cp -r ${rconfigs}/${directory}/tls ${conf}
        fi
    done
}

#
# activates a plugin by copying to $plugindirtmp.
# For this, daemons must have following setting:
# Plugin Directory = @plugindirtmp@
#
enable_plugin()
{
   RC=0
   PLUGIN="$1"

   if [ -z "$PLUGIN" ]; then
      set_error "enable_plugin: no plugin name given."
      exit 1
   fi

   if ! cp $plugindir/${PLUGIN}.* $plugindirtmp/; then
      set_error "enable_plugin: failed to enable ${PLUGIN} plugin."
      RC=1
      exit 1
   fi

   print_debug "plugin ${PLUGIN} enabled"

   return $RC
}

disable_plugin()
{
   RC=0
   PLUGIN="$1"

   if [ -z "$PLUGIN" ]; then
      set_error "enable_plugin: no plugin name given."
      exit 1
   fi

   if ! rm $plugindirtmp/${PLUGIN}.*; then
      set_error "disable_plugin: failed to disable ${PLUGIN} plugin."
      RC=1
      exit 1
   fi

   print_debug "plugin ${PLUGIN} disabled"

   return $RC
}

check_encoding()
{
   ${bin}/susan-dir -d50 -t 2>&1 | grep 'Wanted SQL_ASCII, got UTF8' >/dev/null
   if [ $? = 0 ]; then
       echo "Found database encoding problem, please modify the database encoding (SQL_ASCII)"
       exit 1
   fi
}

cleanup()
{
    if has_tape_drive; then
        ${rscripts}/cleanup-tape
    else
        ${rscripts}/cleanup
    fi
}

#
# Creates a directory "${tmp}/data" from a tgz file.
# This directory can be used a data to backup.
# Initialize ${tmp}/file-list with this directory,
# if it does not already exists.
#
setup_data()
{
    SRC=${1:-data/small.tgz}
    RC=0

    if [ -z "$SRC" ]; then
       set_error "setup_data: no source given."
       return 1
    fi
    if [ ! -e "$SRC" ]; then
        set_error "setup_data $SRC: $SRC not found."
        return 1
    fi

    mkdir -p ${tmp}/data
    (cd ${tmp}/data && tar xzf ${cwd}/$SRC)
    RC=$?

    if [ ! -e ${tmp}/file-list ]; then
       echo "${tmp}/data" >${tmp}/file-list
    fi

    return $RC
}

start_test()
{
   # in case of an exit during the test,
   # call the 'end_test' function.
   trap '
         EXITCODE=$?;
         echo "exit($EXITCODE) is called. Set test to failure and end test.";
         estat=998;
         end_test;
      ' EXIT
   check_encoding
   # Turn off email
   outf="${tmp}/sed_tmp"
   echo "s%  mail =%# mail = %g" >${outf}
   echo "s%  operator =%# operator =%g" >>${outf}
   if [ -f ${conf}/susan-dir.conf ]; then
      cp ${conf}/susan-dir.conf ${tmp}/1
      sed -f ${outf} ${tmp}/1 > ${conf}/susan-dir.conf
   fi
   STARTDATE=`date +%R:%S`
   echo " "
   echo " "
   echo "=== $TestName: starting at $STARTDATE ==="
   echo "=== $TestName: starting at $STARTDATE ===" >> ${working}/log
   echo "="
   echo "="
   export TestName
   export zstat
   export estat
   estat=0
   zstat=0
   bstat=0
   rstat=0
   dstat=0
   # marker for cleanup()
   echo "$STARTDATE" > ${working}/CLEANUPMARKER
}

require_root()
{
MUID=`/usr/bin/id | awk -F= '{print $2}' | awk -F\( '{print $1}'`
if [ $MUID != 0 ] ; then
   echo " "
   echo "You must be root to run this test."
   echo "  ===== !!!! $TestName not run at `date +%R:%S` ==="
   echo "  ===== !!!! $TestName not run at `date +%R:%S` !!!! ===== " >>test.out
   echo " "
   exit 1
fi
}

has_tape_drive()
{
    [ "${TAPE_DRIVE}" ] && [ "${TAPE_DRIVE}" != "/dev/null" ]
    return $?
}

require_tape_drive()
{
if ! has_tape_drive; then
   echo "This test $TestName needs a tape drive, but has none."
   exit 1
fi
}

require_second_drive()
{
if test x${TAPE_DRIVE1} = x/dev/null ; then
   echo "This test $TestName has a Job $JobName which needs a second drive, but has none."
   exit 1
fi
}

require_autochanger()
{
if test x${AUTOCHANGER} = x/dev/null ; then
   echo "This test $TestName needs an autochanger, but has none."
   exit 1
fi
}

require_linux()
{
os=`uname`
if [ $os != 'Linux' ]; then
   echo "This test $TestName runs only on Linux"
   exit 1
fi
}

skip_if_no_autochanger()
{
if test x${AUTOCHANGER} = x/dev/null ; then
   echo "$TestName test skipped. No autochanger."
   exit 1
fi
}

is_debug()
{
  test "$debug" -gt 0
  return $?
}

set_debug()
{
   debug=$1
   if is_debug; then
     out="tee"
   else
     out="output"
   fi
}

print_debug()
{
   echo "$*" | grep -q ERROR > /dev/null
   if test $? -eq 0; then
     echo "$*" >> $tmp/err.log
   fi
   if is_debug; then
     echo "$*" >&2
   fi
}

write_stdin_to_file()
{
  FILE="$1"

  # empty file
  >${FILE}

  # read stdin and write to file
  while read input; do
    printf '%s\n' "$input" >>${FILE}
  done
}

log_stdin()
{
  FILE="${1:-${tmp}/debug.log}"

  # read stdin and write to file
  while read input; do
    # print if debug is set
    print_debug "$input"
    # write to log file
    printf '%s\n' "$input" >>${FILE}
  done
}

set_error()
{
    estat=9
    echo "ERROR: $@" >> $tmp/err.log
    echo "ERROR: $@"
}

check_files_written()
{
    LOG=$1
    NB=$2
    FILES=`awk '/FD Files Written:/ { last=$4 } END { print last }' $LOG`

    if [ "$NB" != "$FILES" ]; then
        print_debug "ERROR: Expect $NB files, get $FILES"
        bstat=2
    fi
}

check_linked_against()
{
   LIB="$1"
   BIN=${2:-${bin}/susan-fd}

   #
   # See if library is linked against libfastlz
   #
   cnt=`ldd ${BIN} 2>/dev/null | grep -c ${LIB}`
   if test ${cnt} -lt 1; then
      print_debug "ERROR: ${BIN} not linked against ${LIB}."
      return 1
   fi

   return 0
}

bls_files_verbose()
{
   local STORAGE=${1}
   local VOLUME=${2}
   # JobId is not yet evaluated
   #local JOBID=${3}
   #local FILENAME=${4}

   ${bin}/bls -V "${VOLUME}" -c ${conf} -v "${STORAGE}"
   return $?
}


check_compression()
{
   local STORAGE=${1}
   local VOLUME=${2}
   # JobId is not yet evaluated
   local JOBID=${3}
   local FILENAME=${4}
   local COMPRESSION=${5:-'GZIP'}
   local COMPRESSION_LEVEL="$6"

   local COMPRESSION_DESCRIPTION="${COMPRESSION}"
   if [ "${COMPRESSION_LEVEL}" ]; then
      COMPRESSION_DESCRIPTION="${COMPRESSION}, level=${COMPRESSION_LEVEL}"
   fi

   print_debug "Is ${FILENAME} compressed with ${COMPRESSION_DESCRIPTION} ?"
   if OUT=$(bls_files_verbose "${STORAGE}" "${VOLUME}" | grep -A1 "| ${FILENAME}$" | grep -i "| ${COMPRESSION_DESCRIPTION}, "); then
      print_debug "$OUT"
   else
      set_error "Use of compression algorithm ${COMPRESSION_DESCRIPTION} in job=${JOBID}, file=${FILENAME} not detected."
   fi
}



################################################################
# Get information from logs
get_mig_info()
{
    # Prev Backup JobId
    JOBID=$1
    LOG=$2
    RET=`awk -F: "BEGIN { jobid=$JOBID } "'/Prev Backup JobId/ { cjid=$2 } /New Backup JobId/  { if (cjid == jobid) { print $2 } }' $LOG`
}

get_duration()
{
   LOG=$1
   RET=`awk 'BEGIN {t["secs"]=1;t["sec"]=1;t["min"]=60;t["mins"]=60}; /Elapsed time:/ { last=$3*t[$4] } END { print last }' $LOG`
}

check_duration()
{
   LOG=$1
   TIME=$2
   OP=${3-gt}

   get_duration $LOG
   if [ "$RET" -$OP "$TIME" ]; then
       print_debug "Expect $OP than $TIME sec, get $RET"
       bstat=2
   fi
}

run_susan()
{
   debug_wait
   zstat=0
   estat=0
   if test "$debug" -eq 1 ; then
     ${scripts}/susan-ctl-dir start -m -d 100
     ${scripts}/susan-ctl-sd start -m -d 100
     ${scripts}/susan-ctl-fd start -m $1 -d 100
   else
     ${scripts}/susan start >/dev/null 2>&1
   fi

   # check daemons
   DAEMON_STATUS_OUT=`${scripts}/susan status`
   DAEMON_STATUS=$?
   print_debug "$DAEMON_STATUS_OUT"

   if [ $DAEMON_STATUS -ne 0 ]; then
      exit 1
   fi

   run_bconsole
   return $?
}

run_bconsole()
{
   bconsole_file=${1:-${tmp}/bconcmds}
   if [ -f $bconsole_file ]; then
      if test "$debug" -eq 1 ; then
        cat $bconsole_file | ${bin}/bconsole -c ${conf}
      else
        cat $bconsole_file | ${bin}/bconsole -c ${conf} 2>&1 >/dev/null
      fi
   fi
   return $?
}

run_btape()
{
   if test "$debug" -eq 1 ; then
     cat ${tmp}/bconcmds | ${bin}/btape -c ${conf} tape | tee ${tmp}/log1.out
   else
     cat ${tmp}/bconcmds | ${bin}/btape -c ${conf} tape >${tmp}/log1.out 2>&1
   fi
}

run_bscan()
{
   if test "$debug" -eq 1 ; then
      ${bin}/bscan -c ${conf} $* | tee ${tmp}/log.out
   else
      ${bin}/bscan -c ${conf} $* 2>&1 >/dev/null
   fi
}

bscan_libdbi()
{
   # examples for LIBDBI settings:
   # LIBDBI="dbdriver = "dbi:postgresql"; dbaddress = 127.0.0.1; dbport = 5432"
   # LIBDBI="dbdriver = "dbi:sqlite"; dbaddress = 127.0.0.1; dbport = 0"

   B=`echo $LIBDBI | sed 's/;//' | sed 's/;//g'`
   B_D=`echo $B | awk '{print $3}'`
   B_t=`echo $B | awk '{print $6}'`
   B_p=`echo $B | awk '{print $9}'`

   BSCANLIBDBI="${LIBDBI:+1}"

   if test "$BSCANLIBDBI" = "1" ; then
      BSCANLIBDBI="-D $B_D -h $B_t -t $B_p"
   else
      BSCANLIBDBI=" "
   fi
}

stop_susan()
{
   if test "$debug" -eq 1 ; then
      ${scripts}/susan stop
   else
      ${scripts}/susan stop 2>&1 >/dev/null
   fi
}

change_files()
{
   #
   # Use this function to modified some files after a full backup
   # so that an incremental backup will see some modified files.
   #
   # Don't rely on specific filenames and paths,
   # as these might change in the future.
   #
   DIR=${1:-${BackupDirectory-}}

   if [ -z "$DIR" ]; then
      print_debug "ERROR: change_files: no directory given."
      return 1
   fi

   if [ ! -d "$DIR" ]; then
      print_debug "ERROR: change_files($DIR): this is not a directory."
      return 1
   fi

   for i in `seq 1 9`; do
      mkdir -p ${DIR}/test$i
      echo "testdata" >> ${DIR}/test$i/test$i.txt
   done

   return 0
}

get_file_size()
{
   FILE="$1"
   SIZE=-1
   if [ -e "$FILE" ]; then
      SIZE=`du "$FILE" | cut -f 1`
   fi
   print_debug "$FILE: $SIZE bytes"
   echo "$SIZE"
}

check_for_zombie_jobs()
{
   ${rscripts}/check_for_zombie_jobs $*
}

change_jobname()
{
   if test $# -eq 1; then
      oldname=NightlySave
      newname=$1
   else
      oldname=$1
      newname=$2
   fi
   rm -f $tmp/1 $tmp/2
   mv ${conf}/susan-dir.conf $tmp/1
   echo "s%${oldname}%${newname}%g" >$tmp/2
   sed -f $tmp/2 $tmp/1 >$conf/susan-dir.conf
#  echo "Job ${oldname} changed to ${newname}"
}

check_two_logs()
{
   grep "^  Termination: *Backup OK" ${tmp}/log1.out 2>&1 >/dev/null
   bstat=${bstat:-$?}
   grep "^  Termination: .*Backup Error" ${tmp}/log1.out 2>&1 >/dev/null
   if test $? -eq 0; then
      bstat=2
   fi
   grep "^  Termination: *Restore OK" ${tmp}/log2.out 2>&1 >/dev/null
   rstat=${rstat:-$?}
   grep "^  Termination: .*Restore Error" ${tmp}/log2.out 2>&1 >/dev/null
   if test $? -eq 0; then
      rstat=2
   fi
   grep "^  Termination: *Restore OK -- warning file count mismatch" ${tmp}/log2.out 2>&1 >/dev/null
   if test $? -eq 0; then
      rstat=3
   fi
   grep "^  Termination: .*Verify Differences" ${tmp}/log2.out 2>&1 >/dev/null
   if test $? -eq 0; then
      rstat=4
   fi
   grep "Encoding error for database" ${tmp}/log1.out > /dev/null
   if test $? -eq 0; then
      print_debug "Found database encoding error"
      bstat=2
   fi
}

check_log()
{
   LOG=$1
   if [ -z "$LOG" ]; then
      LOG=${tmp}/log1.out
   fi

   if ! [ -e "$LOG" ]; then
      set_error "log file $LOG does not exist."
      return 1
   fi

   if grep \
         -e "^  Termination: .*Backup Error" \
         -e "^Can't find " \
         -e "Encoding error for database" \
         -e "^Could not find Client" \
         -e "ERR=" \
         $LOG
   then
      bstat=1
   fi

   if grep \
         -e "^  Termination: .*Restore Error" \
         -e "^  Termination: *Restore OK -- warning " \
         -e "^  Termination: .*Verify Differences" \
         $LOG
   then
      rstat=1
   fi

   if [ $bstat != 0 -o $rstat != 0 ] ; then
      return 1
   fi

   return 0
}

check_restore_diff()
{
   # $dest will be set to
   #   * the first function parameter, or
   #   * ${BackupDirectory} (set by test), or
   #   * ${src}
   dest=${1:-${BackupDirectory:-$src}}

   $rscripts/diff.pl -s ${dest} -d ${tmp}/susan-restores/${dest}
   result=$?
   OUT=`diff -ur ${dest} ${tmp}/susan-restores/${dest}`
   result=`expr "$result" + $?`
   if is_debug; then
       printf "%s\n" "$OUT"
   fi

   if [ $result -ne 0 -a ${dstat:-0} -eq 0 ]; then
      dstat=$result
   fi

   return $result
}

check_restore_only_files_diff()
{
   #
   # all parameter have to be full path files.
   # They will be check for differences to the restore location.
   #
   differences=0
   for i in "$@"; do
      if ! diff -ur "$i" "${tmp}/susan-restores/$i"; then
         differences=`expr $differences + 1`
         dstat=1
      fi
   done

   test $differences -eq 0
   return $?
}

check_restore_files_diff()
{
   if ! check_restore_only_files_diff "$@"; then
      return $?
   fi

   #
   # check if only the files given as parameters have been restored
   #

   # get list of all restored files
   RESTORED_FILES=`find ${tmp}/susan-restores -type f | sed "s%^${tmp}/susan-restores%%"`
   # remove all files given as parameter from the list
   for i in "$@"; do
      RESTORED_FILES=`printf "%s" "$RESTORED_FILES" | grep -v "$i"`
   done
   if [ "$RESTORED_FILES" ]; then
       print_debug "given files: $@"
       print_debug "additional restored files: $RESTORED_FILES"
       set_error "More files then given as parameter have been restored."
       return 1
   fi

   return 0
}

check_restore_bin_diff()
{
   if test "$debug" -eq 1 ; then
      $rscripts/diff.pl -s ${bin} -d ${tmp}/susan-restores${bin}
      diff -ur ${bin} ${tmp}/susan-restores${bin}
   else
      diff -ur ${bin} ${tmp}/susan-restores${bin} 2>&1 >/dev/null
   fi
   dstat=$?
}


check_restore_tmp_build_diff()
{
   if test "$debug" -eq 1 ; then
      $rscripts/diff.pl -s ${tmpsrc} -d ${tmp}/susan-restores${tmpsrc}
      diff -ur ${tmpsrc} ${tmp}/susan-restores${tmpsrc}
   else
      diff -ur ${tmpsrc} ${tmp}/susan-restores${tmpsrc} 2>&1 >/dev/null
   fi
   dstat=$?
}

# bstat is backup error
# dstat is diff difference
# estat is special error status (shown by print_debug message)
# rstat is restore status
# zstat is zombie job(s)
#
end_test()
{
   # End of test.
   # Remove exit trap (set in start_test)
   trap '' EXIT

   if [ x$notracedump != xyes ]; then
      cat ${working}/susan.*.traceback 2>/dev/null
      cp -f  ${working}/susan.*.traceback ${dumps} 2>/dev/null
      cat ${working}/*.bactrace 2>/dev/null
      cp -f ${working}/*.bactrace ${dumps} 2>/dev/null
   fi
   if [ -f $tmp/err.log ]; then
      cat $tmp/err.log
   fi
   ENDDATE=`date +%R:%S`
   if [ $estat != 0 ] ; then
      echo " "
      echo "  !!!!! $TestName failed!!! $ENDDATE !!!!! "
      echo "   Status: estat=$estat zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
      echo "  !!!!! $TestName failed!!! $ENDDATE !!!!! " >>test.out
      echo "   Status: estat=$estat zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
      echo " "
      exit 1
   fi
   if [ $zstat != 0 ] ; then
      echo " "
      echo "  !!!!! $TestName failed!!! $ENDDATA !!!!! "
      echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
      echo "  !!!!! $TestName failed!!! $ENDDATE !!!!! " >>test.out
      echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
      echo " "
      exit 1
   fi
   if [ $dstat != 0 -o $bstat != 0 -o $rstat != 0 ] ; then
      echo " "
      echo " "
      echo "  !!!!! $TestName failed!!! $ENDDATE !!!!! "
      echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat"
      echo "  !!!!! $TestName failed!!! $ENDDATE !!!!! " >>test.out
      echo "   Status: zombie=$zstat backup=$bstat restore=$rstat diff=$dstat" >>test.out
      if [ $bstat != 0 -o $rstat != 0 ] ; then
         echo "  !!!!! Bad termination status       !!!!! "
         echo "  !!!!! Bad termination status       !!!!! " >>test.out
      else
         echo "  !!!!! Restored files differ          !!!!! "
         echo "  !!!!! Restored files differ          !!!!! " >>test.out
      fi
      echo "   Status: backup=$bstat restore=$rstat diff=$dstat"
      echo "   Status: backup=$bstat restore=$rstat diff=$dstat" >>test.out
      echo " "
      exit 1
   else
      echo "="
      echo "="
      echo "=== $TestName: OK at $ENDDATE === "
      echo "=== $TestName: OK at $ENDDATE === " >>test.out
      #if ! is_debug; then
      #   ${rscripts}/cleanup
      #fi
   fi
}

copy_tape_confs()
{
   ${rscripts}/cleanup-tape
   ${rscripts}/copy-tape-confs
}

copy_test_confs()
{
   ${rscripts}/cleanup
   ${rscripts}/copy-test-confs
}

disable_plugins()
{
   for i in ${conf}/susan-fd.conf; do
      sed 's/Plugin/#Plugin/' $i > $tmp/1
      cp -f $tmp/1 $i
   done
}

update_win32()
{
   if [ -d $cwd/build/src/win32/release32   \
     -a -d $cwd/build/src/win32/release64 ] \
   || [ -d $cwd/release32 -a -d $cwd/release64 ]
   then
       echo -ne "Try to upgrade the FileDaemon:\t"
       wget -qO - "$WIN32_ADDR:8091/install"
   fi
}

debug_wait()
{
  if test "x${REGRESS_WAIT}" = "x1"; then
     echo "Start Susan under debugger and enter anything when ready ..."
     read a
  fi
}

init_drive()
{
  mt -f $1 rewind
  mt -f $1 weof
}

rewind_drive()
{
  mt -f $1 rewind
}

load_slot1()
{
# Get a tape from slot1
slot=`${scripts}/$MTX ${AUTOCHANGER} loaded 0 ${TAPE_DRIVE} $DRIVE1`
case $slot in
 0)
    ${scripts}/$MTX ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
    slot=$SLOT1
    ;;
 $SLOT1)
    slot=$SLOT1
    ;;
 *)
    rewind_drive ${TAPE_DRIVE}
    ${scripts}/$MTX ${AUTOCHANGER} unload $slot  ${TAPE_DRIVE} $DRIVE1
    ${scripts}/$MTX ${AUTOCHANGER} load   $SLOT1 ${TAPE_DRIVE} $DRIVE1
    slot=$SLOT1
    ;;
esac
}

#
# $1 has currently loaded slot, load the other one i.e. if 1, load 2;
#    if 2, load 1; if 0 load 1
#
load_other_slot()
{
rewind_drive ${TAPE_DRIVE}
case $1 in
 0)
    ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
    slot=1
    ;;
 $SLOT1)
    ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
    ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT2 ${TAPE_DRIVE} $DRIVE1
    slot=2
    ;;
 $SLOT2)
    ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} unload $1 ${TAPE_DRIVE} $DRIVE1
    ${scripts}/${AUTOCHANGER_SCRIPT} ${AUTOCHANGER} load $SLOT1 ${TAPE_DRIVE} $DRIVE1
    slot=1
    ;;
 *)
    echo "Something went wrong. Expected $SLOT1 or $SLOT2, got $1"
    exit 1
    ;;
esac
}


if test "x${REGRESS_DEBUG}" = "x1"; then
   set_debug 1
else
   set_debug 0
fi

# Save current directory
cwd=`pwd`

# Source the configuration variables
. ${cwd}/config

db_name=${db_name:-"regress"}
db_user=${db_user:-"regress"}
db_password=${db_password:-""}
working=${working:-"$cwd/working"}
dumps=${dumps:-"$cwd/dumps"}
bin=${bin:-"$cwd/bin"}
tmp=${tmp:-"$cwd/tmp"}

# Susan scripts
scripts=${scripts:-"$cwd/bin"}

# Susan Plugin Directory
plugindir=${plugindir:-"$cwd/bin/plugins"}
# some tests (BASE) load only the plugins copied to plugindirtmp,
# to avoid that all plugins get loaded.
plugindirtmp=${plugindirtmp:-"$working/plugins"}

# Susan conf files
conf=${conf:-"$cwd/bin"}
confdir="$conf"
configs="$conf"
SUSAN_CONFIG_DIR="$conf"

# Regress scripts
rscripts=${rscripts:-"$cwd/scripts"}

# Regress configs
rconfigs=${rconfigs:-"$cwd/configs"}


# Susan source directory when copied here
#  also build directory
src=${src:-"$cwd/build"}

# Temp source directory so we don't mess up $src
tmpsrc=${tmpsrc:-"$cwd/tmp/build"}

export SUSAN_CONFIG_DIR
export bin
export conf
export confdir
export configs
export dumps
export plugindir
export plugindirtmp
export rscripts
export scripts
export src
export tmp
export tmpsrc
export working

export dirport=$BASEPORT
export fdport=$(($BASEPORT + 1))
export sdport=$(($BASEPORT + 2))
export SUSAN_DIR_PORT=$dirport
export SUSAN_FD_PORT=$fdport
export SUSAN_SD_PORT=$sdport

export PERLLIB="$cwd"
export PERL5LIB="$cwd"
bperl="perl -Mscripts::functions"
export bperl

mkdir -p ${working}
mkdir -p ${tmp}
mkdir -p ${plugindirtmp}
touch ${tmp}/dir.out ${tmp}/fd.out ${tmp}/sd.out

CLIENT=${HOST}-fd

AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
LD_LIBRARY_PATH=$bin:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

trap "{ estat=999; end_test; }" TERM
